---@class Battle.ActorManager
local ActorManager = Battle.CreateClass("ActorManager")
Battle.ActorManager = ActorManager
local this = ActorManager
function ActorManager.OnCreate()
    ---@type Battle.Entity[]
    this.actorList = {}     --场景视野/屏幕之内的玩家
    ---@type number[]
    this.delayDestroyList = {}
    ---@type Battle.Entity[]
    this.waitingLoadList = {}
    ---@type Battle.Entity[]
    this.loadCountPer = 2   --单次加载数量
    this.lordList = {}
    this.isStartLogic = false
    Battle.ClientBattle:RegisterUpdateCallback(this.Update)
    this.tempList = {}
end
function ActorManager.OnDestroy()
    if this.actorList then
        for k,v in pairs(this.actorList) do
            v:BaseDestroy()
        end
    end
    this.delayDestroyList = nil
    this.actorList = nil
    this.waitingLoadList = nil
    this.lordList = nil
end
--战斗逻辑开始运行，技能cd开始更新
function ActorManager.StartLogic()
    this.isStartLogic = true
end

---@return Battle.Entity
function ActorManager.GetClickActor(hitTran)
    for k,v in pairs(this.actorList) do
        if v.gameObjectSystem:IsTran(hitTran) then
            return v
        end
    end
    return nil
end

---@param actor Battle.Entity
function ActorManager.AddActor(actor)
    if actor == nil then return end
    local a = this.GetActor(actor:GetId())
    if a ~= nil then
       -- logError("尝试添加一个重复的actor:"..actor:GetId())
        return
    end
    this.actorList[actor:GetId()] = actor
    this.waitingLoadList[actor:GetId()] = actor
end
function ActorManager.DelayDestroyActor(id)
    this.delayDestroyList[id] = id
end
---@param actor Battle.Entity
function ActorManager.DestroyActor(actor)
    if not actor then return end
    Battle.GlobalEventSystem.BroadCast(Battle.Event.ActorDestroy,Battle.EventArgs.EntityArgs:Create(actor))
    actor:BaseDestroy()
    this.actorList[actor:GetId()] = nil
end

---@return Battle.Entity
function ActorManager.GetActor(entityId)
    if not this.actorList then return nil end
    local actor = this.actorList[entityId]
    return actor
end
---@return Battle.Entity[]
function ActorManager.GetActorList()
    return this.actorList
end
---@return Battle.Entity
function ActorManager.AddLord(camp,actor)
    this.lordList[camp] = actor
end
---@return Battle.Entity
function ActorManager.GetLord(camp)
    return this.lordList[camp]
end
---@return Battle.Entity[]
function ActorManager.GetActorListByCamp(campId)
    table.clear(this.tempList)
    for k,v in pairs(this.actorList) do
        if v.camp == campId then
            table.insert(this.tempList,v);
        end
    end
    return this.tempList;
end
---@return Battle.Entity[]
function ActorManager.GetActorListBySign(campId,signId)
    table.clear(this.tempList)
    for k,v in pairs(this.actorList) do
        if v.camp == campId and v.signSystem:HasSign(signId) then
            table.insert(this.tempList,v);
        end
    end
    return this.tempList;
end

function ActorManager.Update(dt)
    --checkDead
    for k,v in pairs(this.delayDestroyList) do
        this.DestroyActor(this.GetActor(k))
        this.waitingLoadList[k] = nil
        this.delayDestroyList[k] = nil
    end
    if this.isStartLogic then
        for k,v in pairs(this.actorList) do
            v:Update(dt)
        end
    end
    --loading
    local count = 0
    for k,v in pairs(this.waitingLoadList) do
        v:LoadModel()
        count = count + 1
        this.waitingLoadList[v:GetId()] = nil
        if count >= this.loadCountPer then
            break
        end
    end
end
